package cn.iocoder.an.module.business.controller.admin.auditplan;

import cn.iocoder.an.module.business.dal.dataobject.auditplanarea.AuditPlanAreaDO;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.auditplan.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.auditplan.AuditPlanDO;
import cn.iocoder.an.module.business.service.auditplan.AuditPlanService;

@Tag(name = "管理后台 - 审核计划")
@RestController
@RequestMapping("/business/audit-plan")
@Validated
public class AuditPlanController {

    @Resource
    private AuditPlanService auditPlanService;


    @TransMethodResult
    @PostMapping("/createTask")
    @Operation(summary = "创建任务")
    public CommonResult<Boolean> createAuditTask(@RequestBody AuditPlanSaveReqVO createReqVO) {
        return success(auditPlanService.createAuditTask(createReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建审核计划")
    @PreAuthorize("@ss.hasPermission('business:audit-plan:create')")
    public CommonResult<Long> createAuditPlan(@Valid @RequestBody AuditPlanSaveReqVO createReqVO) {
        return success(auditPlanService.createAuditPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新审核计划")
    @PreAuthorize("@ss.hasPermission('business:audit-plan:update')")
    public CommonResult<Boolean> updateAuditPlan(@Valid @RequestBody AuditPlanSaveReqVO updateReqVO) {
        auditPlanService.updateAuditPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:audit-plan:delete')")
    public CommonResult<Boolean> deleteAuditPlan(@RequestParam("id") Long id) {
        auditPlanService.deleteAuditPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除审核计划")
                @PreAuthorize("@ss.hasPermission('business:audit-plan:delete')")
    public CommonResult<Boolean> deleteAuditPlanList(@RequestParam("ids") List<Long> ids) {
        auditPlanService.deleteAuditPlanListByIds(ids);
        return success(true);
    }


    @TransMethodResult
    @GetMapping("/get")
    @Operation(summary = "获得审核计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:audit-plan:query')")
    public CommonResult<AuditPlanRespVO> getAuditPlan(@RequestParam("id") Long id) {
        AuditPlanDO auditPlan = auditPlanService.getAuditPlan(id);
        return success(BeanUtils.toBean(auditPlan, AuditPlanRespVO.class));
    }

    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得审核计划分页")
    @PreAuthorize("@ss.hasPermission('business:audit-plan:query')")
    public CommonResult<PageResult<AuditPlanRespVO>> getAuditPlanPage(@Valid AuditPlanPageReqVO pageReqVO) {
        PageResult<AuditPlanDO> pageResult = auditPlanService.getAuditPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuditPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出审核计划 Excel")
    @PreAuthorize("@ss.hasPermission('business:audit-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuditPlanExcel(@Valid AuditPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuditPlanDO> list = auditPlanService.getAuditPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "审核计划.xls", "数据", AuditPlanRespVO.class,
                        BeanUtils.toBean(list, AuditPlanRespVO.class));
    }

    // ==================== 子表（审核计划区域） ====================

    @TransMethodResult
    @GetMapping("/audit-plan-area/list-by-plan-id")
    @Operation(summary = "获得审核计划区域列表")
    @Parameter(name = "planId", description = "计划")
    @PreAuthorize("@ss.hasPermission('business:audit-plan:query')")
    public CommonResult<List<AuditPlanAreaDO>> getAuditPlanAreaListByPlanId(@RequestParam("planId") Long planId) {
        return success(auditPlanService.getAuditPlanAreaListByPlanId(planId));
    }

    @TransMethodResult
    @GetMapping("/audit-plan-area/areaList")
    @Operation(summary = "获得审核计划区域列表")
    public CommonResult<List<AuditPlanAreaDO>> getAuditPlanAreaListByPlanId() {
        return success(auditPlanService.getAuditAreaList());
    }

}